from django.db import models


# class Shop(models.Model):
#     name = models.CharField(verbose_name='店铺名称', max_length=200)
#     places = models.CharField(verbose_name='店铺地址', max_length=200)
#     rate = models.FloatField(verbose_name='店铺评分', default=5.0)
#     icon = models.ImageField(verbose_name='店铺图标', null=True, blank=True, default='')
#
#     class Meta:
#         db_table = 'Shop'


# class User(models.Model):
#     user_type_choices = (
#         ('1', '普通用户'),
#         ('2', '卖家'),
#         ('3', '管理员')
#     )
#     user_type = models.IntegerField(verbose_name='用户类型', choices=user_type_choices, default=1)
#     user_name = models.CharField(verbose_name='用户名', max_length=64, default='')
#     password = models.CharField(verbose_name='密码', max_length=32, default='123')
#     shop = models.ForeignKey(related_name='user_shop', to='Shop', on_delete=models.CASCADE, blank=True, null=True)
#
#     class Meta:
#         db_table = 'User'


# class UserToken(models.Model):
#     # 登录时候的token
#     user = models.OneToOneField(to='User', on_delete=models.CASCADE)
#     token = models.CharField(max_length=64)
#
#     class Meta:
#         db_table = 'UserToken'


class Cover(models.Model):
    """
    商品封面信息
    """
    name = models.CharField(verbose_name='商品名称', max_length=200)  # 商品名称
    tag = models.CharField(verbose_name='商品标签', max_length=200)  # 商品标签
    src = models.ImageField(verbose_name='商品封面图片', null=True, blank=True, )  # 商品封面图片路径
    description = models.CharField(verbose_name='商品描述', max_length=200, default='', null=True, blank=True)
    price = models.DecimalField(verbose_name='价格', decimal_places=2, max_digits=6, default='')
    sales = models.IntegerField(verbose_name='销量', default=0)
    recommended = models.BooleanField(verbose_name='是否推荐', default=0)
    disabled = models.BooleanField(verbose_name='是否下架', default=0)

    # 和Detail一对一关联

    class Meta:
        ordering = ['id']
        db_table = 'Cover'


class Detail(models.Model):
    """
    商品详情信息
    """
    # 与封面信息建立一对一关系
    cover = models.OneToOneField(verbose_name='封面', to='Cover', on_delete=models.CASCADE, primary_key=True)  # 封面信息
    composition = models.CharField(verbose_name='成分', max_length=200, default='', null=True, blank=True)
    shop = models.ForeignKey(to='Window', related_name='detail_shop', on_delete=models.CASCADE, default='', null=True, )
    rate = models.FloatField(verbose_name="商品评分", default=5.0)

    # 商品详情图片

    class Meta:
        db_table = 'Detail'


class DetailSrc(models.Model):
    """
    商品详情图片路径
    """
    detail = models.ForeignKey(Detail, related_name='detailsrc', on_delete=models.CASCADE)  # 详情
    src = models.ImageField(verbose_name='商品详情图片')

    class Meta:
        db_table = 'DetailSrc'


gender_choices = [
    (1, '男'),
    (2, '女')
]


class User(models.Model):
    session_key = models.CharField(verbose_name='微信会话秘钥', max_length=32)  # 不可向前端传递
    openid = models.CharField(verbose_name='微信用户唯一标识', max_length=32)  # 不可向前端传递
    token = models.CharField(verbose_name='token', default='', max_length=255)
    studentid = models.CharField(verbose_name='学号', max_length=64, blank=True, null=True, default='', unique=True)
    password = models.CharField(max_length=32, default="123456")
    name = models.CharField(verbose_name='姓名', max_length=32, blank=True, null=True, default='')

    nickname = models.CharField(verbose_name='微信昵称', max_length=64)
    avatar = models.ImageField(verbose_name='微信头像', null=True)
    role = models.CharField(verbose_name="用户身份", default='本科生', max_length=10)
    # 可以为空
    gender = models.IntegerField(verbose_name='性别', choices=gender_choices, blank=True, null=True)
    age = models.IntegerField(verbose_name='年龄', blank=True, null=True)
    phone = models.CharField(verbose_name='手机号', blank=True, max_length=32, null=True)
    email = models.EmailField(verbose_name='邮箱', blank=True, null=True)
    disabled = models.BooleanField(default=False, verbose_name="禁用标志")
    class Meta:
        db_table = 'user'


class Staff(models.Model):
    session_key = models.CharField(verbose_name='微信会话秘钥', max_length=32)  # 不可向前端传递
    openid = models.CharField(verbose_name='微信用户唯一标识', max_length=32)  # 不可向前端传递
    token = models.CharField(verbose_name='token', default='', max_length=255)
    nickname = models.CharField(verbose_name='微信昵称', max_length=64)
    avatar = models.ImageField(verbose_name='微信头像', null=True)

    workid = models.CharField(verbose_name='工号', max_length=64, blank=True, null=True, default='', unique=True)
    password = models.CharField(max_length=32, default="123456")
    # 可以为空
    gender = models.IntegerField(verbose_name='性别', choices=gender_choices, blank=True, null=True)
    age = models.IntegerField(verbose_name='年龄', blank=True, null=True)
    phone = models.CharField(verbose_name='手机号', blank=True, max_length=32, null=True)
    email = models.EmailField(verbose_name='邮箱', blank=True, null=True)
    shop = models.ForeignKey(to='Window', on_delete=models.CASCADE, verbose_name='店铺', blank=True, default='',
                             null=True, )
    disabled = models.BooleanField(default=False, verbose_name="禁用标志")

    class Meta:
        db_table = 'staff'


class OrderInfo(models.Model):
    ORDER_STATUS_CHOICES = (
        (1, "待支付"),
        (2, "待接单"),
        (3, "制作中"),
        (4, "待取餐"),
        (5, "已完成"),
        (6, "已取消"),
    )
    order_sn = models.CharField(verbose_name='订单编号', unique=True, max_length=30)
    user_id = models.ForeignKey(to='User', related_name='orderid', on_delete=models.CASCADE)
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间")
    receive_time = models.DateTimeField(null=True, blank=True, verbose_name="接单时间")
    cookok_time = models.DateTimeField(null=True, blank=True, verbose_name='制作完成时间')
    pick_time = models.DateTimeField(null=True, blank=True, verbose_name='取餐时间')
    orderok_time = models.DateTimeField(null=True, blank=True, verbose_name='订单完成时间')
    cancel_time = models.DateTimeField(null=True, blank=True, verbose_name='订单取消时间')
    total_count = models.IntegerField(default=1, verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")
    # 多对多字段，自定义中间表
    goods = models.ManyToManyField(to='Detail', through='OrderGoods')
    deleted = models.BooleanField(default=False, verbose_name="是否删除")
    shop = models.ForeignKey(to='Window', to_field='id', related_name='order', on_delete=models.CASCADE, blank=True,
                             null=True)

    class Meta:
        db_table = "order_info"
        verbose_name = "订单基本信息"
        ordering = ['id']


class OrderGoods(models.Model):
    SCORE_CHOICES = (
        (0, "0分"),
        (1, "20分"),
        (2, "40分"),
        (3, "60分"),
        (4, "80分"),
        (5, "100分"),
    )
    orderinfo = models.ForeignKey(to='OrderInfo', on_delete=models.CASCADE, verbose_name='订单信息',
                                  related_name='ordergoods')
    detail = models.ForeignKey(to='Detail', on_delete=models.CASCADE, verbose_name='商品')
    # 添加额外字段
    remarks = models.CharField(max_length=200, verbose_name='用户备注', default='')
    num = models.IntegerField(default=0, verbose_name='商品数量')
    score = models.SmallIntegerField(choices=SCORE_CHOICES, null=True, blank=True, verbose_name="满意度评分")
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价了')
    comment = models.TextField(default="", verbose_name="评价信息")

    class Meta:
        db_table = 'order_goods'
        verbose_name = '订单商品'


class ShoppingCart(models.Model):
    user = models.ForeignKey(User, verbose_name="用户", on_delete=models.CASCADE, related_name='cart')
    goods = models.ForeignKey(Cover, verbose_name="商品", on_delete=models.CASCADE)
    num = models.IntegerField(default=1, verbose_name="购买数量")
    checked = models.BooleanField(default=False, verbose_name="是否被选中")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    deleted = models.BooleanField(default=False, verbose_name="是否删除")

    class Meta:
        db_table = 'shopping_cart'
        verbose_name = '购物车'
        verbose_name_plural = verbose_name
        # unique_together = ("user", "goods")  # 一个商品不应该在购物车中重复

    def __str__(self):
        return "%s(%d)".format(self.goods.name, self.num)


class Admins(models.Model):
    username = models.CharField(max_length=64)
    email = models.CharField(max_length=64)
    phone = models.CharField(max_length=11)
    password = models.CharField(max_length=32, default="123456")
    flag = models.IntegerField(default=0)
    code = models.CharField(max_length=16, blank=True)

    class Meta:
        db_table = 'Admins'
        verbose_name = '食堂管理员'


class Window(models.Model):
    title = models.CharField(max_length=64, verbose_name="店铺名")
    type = models.CharField(max_length=32, verbose_name="店铺类型")
    sales = models.IntegerField(default=0, verbose_name="销量")
    rate = models.FloatField(verbose_name="店铺评分", default=0)
    brief = models.CharField(max_length=64, verbose_name="店铺简介", blank=True, null=True)
    position = models.CharField(max_length=64, verbose_name="店铺地址", blank=True, null=True)
    connect = models.CharField(max_length=16, verbose_name="联系电话", blank=True, null=True)
    disabled = models.BooleanField(default=False, verbose_name="店铺禁用标志")
    icon = models.ImageField(verbose_name='店铺图标', null=True, blank=True, default='')
    buisness = models.BooleanField(verbose_name='是否营业',default=True)

    class Meta:
        db_table = 'Window'
        verbose_name = '店铺'


class Photo_Window(models.Model):
    owner = models.ForeignKey(Window, related_name="photos", on_delete=models.CASCADE)
    src = models.CharField(max_length=200, verbose_name="图片地址")

    class Meta:
        db_table = 'Photo_Window'
        verbose_name = '店铺轮播图'


class Record_Day(models.Model):
    good = models.ForeignKey('Cover', related_name="g_record", on_delete=models.CASCADE, verbose_name="菜品")
    shop = models.ForeignKey('Window', related_name="s_record", on_delete=models.CASCADE, verbose_name="店铺")
    record_time = models.DateField(auto_now_add=True, verbose_name="记录时间")
    sales = models.IntegerField(default=0, verbose_name="日销量")
    amount = models.FloatField(default=0, verbose_name="销售额")
    value = models.FloatField(verbose_name="评分", default=0)

    class Meta:
        db_table = 'Record_Day'
        verbose_name = '日销量'


class Notice(models.Model):
    STATUS_CHOICE = (
        (1, "未发布"),
        (2, "发布"),
        (3, "删除"),
        (4, "已过期"),
    )
    title = models.CharField(max_length=40, verbose_name='公告标题')
    content = models.TextField(verbose_name='公告内容')
    publishTime = models.DateTimeField(verbose_name='发布时间', null=True, blank=True)
    holdTime = models.DateTimeField(verbose_name='保存时间', null=True, blank=True)
    status = models.SmallIntegerField(choices=STATUS_CHOICE, default=1, verbose_name="状态")

    class Meta:
        abstract = True
        db_table = 'Notice'
        verbose_name = '公告'


class S_Notice(Notice):
    window = models.ForeignKey(to='Window', related_name='notice', on_delete=models.CASCADE)
    staff = models.ForeignKey(to='Staff', related_name='notice', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '餐厅公告'
        db_table = 'S_Notice'


class C_Notice(Notice):
    admin = models.ForeignKey(to='Admins', related_name='notice', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '食堂公告'
        db_table = 'C_Notice'


class NoticeImages(models.Model):
    src = models.ImageField(verbose_name='公告图片')
    notice = models.ForeignKey(to='S_Notice', related_name='image', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '公告图片'
        db_table = 'NoticeImages'
